package com.example.opengldemo._01_;

import android.opengl.GLES30;
import android.opengl.GLES30;
import android.opengl.Matrix;

import com.example.opengldemo.util.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Triangle {
    public static float[] mPMatrix = new float[16];
    public static float[] mVMatrix = new float[16];
    public static float[] mMMatrix = new float[16] ;
    public static float[] mMVPMatrix = new float[16];

    int mProgram ;
    int m_U_MVPHandle ;
    int m_A_PositionHandle ;
    int m_A_ColorHandle ;

    String mVertexShader ;
    String mFragmentShader ;

    FloatBuffer mVertexBuffer ;
    FloatBuffer mColorBuffer ;

    int vCount ;
    float xAngle ;

    public Triangle(MyTDView myTDView){
        initVertexData();
        initShader(myTDView) ;
    }
    private void initVertexData() {
        vCount = 3 ;
        final float UNIT_SIZE = 0.2f ;
        //vertex array
        float[] vertexs = new float[]{
                -4*UNIT_SIZE , 0 , 0 ,
                0 , -4*UNIT_SIZE , 0 ,
                +4*UNIT_SIZE , 0 , 0
        };
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexs.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        mVertexBuffer = byteBuffer.asFloatBuffer() ;
        mVertexBuffer.put(vertexs);
        mVertexBuffer.position(0);
        // color array
        float[] colorArray = new float[]{
                1,1,1,0,
                0,0,1,0,
                0,1,0,0
        };
        byteBuffer = ByteBuffer.allocateDirect(colorArray.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        mColorBuffer = byteBuffer.asFloatBuffer();
        mColorBuffer.put(colorArray);
        mColorBuffer.position(0) ;
    }

    private void initShader(MyTDView myTDView) {
        mVertexShader = ShaderUtil.loadFromAssetsFile("vertex.glsl" , myTDView.getResources());
        mFragmentShader = ShaderUtil.loadFromAssetsFile("fragment.glsl" , myTDView.getResources());
        mProgram = ShaderUtil.createProgram(mVertexShader , mFragmentShader);

        initHandle(mProgram) ;
    }

    private void initHandle(int mProgram) {
        m_A_PositionHandle = GLES30.glGetAttribLocation(mProgram , "aPosition") ;
        m_A_ColorHandle = GLES30.glGetAttribLocation(mProgram , "aColor") ;
        m_U_MVPHandle = GLES30.glGetUniformLocation(mProgram , "uMVPMatrix");
    }


    public void drawSelf(){
        GLES30.glUseProgram(mProgram);

        Matrix.setRotateM(mMMatrix,0,0,0,1,0);
        Matrix.translateM(mMMatrix,0,0,0,1);
        Matrix.rotateM(mMMatrix,0,xAngle,1,0,0);

//        float[] finalMatrix = getFinalMatrix(mMMatrix);
//        GLES30.glUniformMatrix4fv(m_U_MVPHandle , 1 , false , finalMatrix ,0);

        GLES30.glVertexAttribPointer(m_A_PositionHandle , 3 , GLES30.GL_FLOAT , false , 3*4 , mVertexBuffer);
        GLES30.glVertexAttribPointer(m_A_ColorHandle , 4 , GLES30.GL_FLOAT , false , 4*4 , mColorBuffer);

        GLES30.glEnableVertexAttribArray(m_A_ColorHandle);
        GLES30.glEnableVertexAttribArray(m_A_PositionHandle);

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES , 0 , vCount);

        GLES30.glDisableVertexAttribArray(m_A_PositionHandle);
        GLES30.glDisableVertexAttribArray(m_A_ColorHandle);


    }
    public static float[] getFinalMatrix(float[] spec){
        Matrix.multiplyMM(mMVPMatrix,0,mVMatrix,0,spec,0);
        Matrix.multiplyMM(mMVPMatrix,0,mPMatrix,0,mMVPMatrix,0);
        return mMVPMatrix  ;

    }

}
